package com.tencent.wuyanzu.whattoeat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.wuyanzu.whattoeat.Entity.Food;
import com.tencent.wuyanzu.whattoeat.Global.ARGS;
import com.tencent.wuyanzu.whattoeat.net.NetUtil;

import com.tencent.wuyanzu.whattoeat.util.ParseJSONFoodData;

import com.tencent.wuyanzu.whattoeat.util.ViewImageLoadURL;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;


public class SelectActivity extends Activity {
    private ImageView foodImageView;
    private TextView  foodNameView;
    private TextView thurmUp;
    private TextView thurmDown;

    private  Button sameWithLast;
    private Button confirmButton;
    private int idx = 0;
    final  String TAG="SelectActivity";
    private List<Food> foods=null;
    private JSONArray foodsJsonArray=null;
    private Handler handler=new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            int msgWhat=msg.what;
            switch (msgWhat) {
                case 1://请求推荐列表
                    try
                    {
                        String receiveString=msg.getData().getString("result");
                        Log.i(TAG,receiveString);
                        JSONObject receiveJson=new JSONObject(receiveString);
                        foodsJsonArray=receiveJson.getJSONArray("food");
                        foods= ParseJSONFoodData.getFoodFromJsonArray(foodsJsonArray);

                        idx=0;
                        setFoodInformation(idx);

                    }catch (Exception e)
                    {
                        e.printStackTrace();
                    }

                    break;

                default:
                    Log.i(TAG,"unsupport msgWhat");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select);


        confirmButton = (Button) findViewById(R.id.search_chooseIt);
        sameWithLast=(Button)findViewById(R.id.search_same_last);
        confirmButton.setOnClickListener(conformlistener);
        sameWithLast.setOnClickListener(sameWithLastlistener);
        foodImageView = (ImageView)findViewById(R.id.select_food_image);

        foodNameView=(TextView)findViewById(R.id.select_food_name);

        thurmUp=(TextView)findViewById(R.id.select_thurm_up_number);
        thurmDown=(TextView)findViewById(R.id.select_thurm_down_number);

            foodImageView.setOnTouchListener(new View.OnTouchListener() {
                float posX,posY;
                float currentPosX,currentPosY;
                boolean oncemore = true;

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            posX = event.getX();
                            posY = event.getY();
                            Log.d("down","X:"+posX+",Y:"+posY);
                            break;

                        case MotionEvent.ACTION_UP:
                            currentPosX = event.getX();
                            currentPosY = event.getY();
                            Log.d("up","X:"+currentPosX+",Y:"+currentPosY);
                            int flag = 0;
                            if (currentPosX - posX > 100) {
                                flag = 1; //right
                            }
                            else if (currentPosX - posX < -100) {
                                flag = -1;
                            }
                            else//仅仅是点击事件
                            {

                                Intent intent = new Intent(SelectActivity.this,FoodDetailActivity.class);
                                intent.putExtra("foodDetail",foods.get(idx));
                                intent.putExtra("isChoose",false);

                                startActivity(intent);
                            }
                            if(flag==1 && idx > 0 && oncemore) {
                                oncemore = false;
                                idx--;

                                try {
                                    setFoodInformation(idx);
                                }catch (Exception e)
                                {
                                    e.printStackTrace();
                                }

                            } else if(flag == -1) {
                                if(idx < foodsJsonArray.length()-1) {
                                    oncemore = true;
                                    idx++;
                                    try {
                                        setFoodInformation(idx);
//                                        ViewImageLoadURL bitmapLoadURL=new ViewImageLoadURL();
//                                        bitmapLoadURL.execute(foodsJsonArray.getJSONObject(idx).getString("url"), foodImageView);
//                                        foodNameView.setText(foodsJsonArray.getJSONObject(idx).getString("name"));
                                    }catch (Exception e)
                                    {
                                        e.printStackTrace();
                                    }
                                }  else { // last one
                                    Intent mainIntent = new Intent(SelectActivity.this,AnotherChoiceActivity.class);
                                    SelectActivity.this.startActivity(mainIntent);
                                    SelectActivity.this.finish();
                                }
                            }
                            break;
                        default:
                            break;
                    }
                    return true;
                }
            });

    }

    Button.OnClickListener conformlistener = new Button.OnClickListener(){
        public void onClick(View view){
           // String foodName = findViewById(R.id.select_food_name).toString();
            Intent intent = new Intent(SelectActivity.this,FoodDetailActivity.class);
            intent.putExtra("foodDetail",foods.get(idx));
            intent.putExtra("isChoose",true);
            startActivity(intent);
        }
    };

    Button.OnClickListener sameWithLastlistener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Context context=SelectActivity.this;
            Intent intent = new Intent(context,MealTimeActivity.class);
            SharedPreferences sharedPreferences=context.getSharedPreferences("whattoeat", Activity.MODE_PRIVATE);
            Food selectFood=new Food();
            selectFood.setName(sharedPreferences.getString("name","null"));
            if(selectFood.getName().equals("null"))
            {
                Toast.makeText(context,"您没有选过菜",Toast.LENGTH_LONG).show();
                return;
            }
            selectFood.setUrl(sharedPreferences.getString("url","null"));

            selectFood.setUp(sharedPreferences.getInt("up",-1));
            selectFood.setDown(sharedPreferences.getInt("down",-1));
            selectFood.setPhoneNumber(sharedPreferences.getString("phoneNumber","null"));
            selectFood.setAddress(sharedPreferences.getString("address","null"));
            String address=sharedPreferences.getString("address","null");
            selectFood.setAddress(address);
            intent.putExtra("foodDetail",selectFood);

            context.startActivity(intent);
            finish();
        }
    };

    private void setFoodInformation(int idx)
    {
        try
        {
            ViewImageLoadURL bitmapLoadURL=new ViewImageLoadURL();
            bitmapLoadURL.execute(foods.get(idx).getUrl(), foodImageView);
            foodNameView.setText(foods.get(idx).getName());

            thurmDown.setText(""+foods.get(idx).getDown());
            thurmUp.setText(""+foods.get(idx).getUp());

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }


    @Override
    protected void onResume() {
        super.onResume();
        NetUtil.executeGet("/recommend?uid="+ ARGS.UID,handler,1);
    }
}
